<?php


namespace App\Services;

use App\Models\BookShelf;
use App\Models\Novel;

class ShelfService
{
    public function shelfList($userId)
    {
        $novelIds = BookShelf::query()->where('user_id', $userId)->get()->pluck('novel_id')->toArray();
        $novels = Novel::query()->whereIn('id', $novelIds)->where('is_hide', 'N')
            ->select('id as novel_id', 'novel_name', 'cover_url')
            ->get()->toArray();

        return toCamelCaseKey($novels);
    }

    public function up($novelId, $userId)
    {
        $exist = BookShelf::query()->where('user_id', $userId)
            ->where('novel_id', $novelId)
            ->exists();
        if ($exist) {
            return true;
        }
        BookShelf::query()->create([
            'user_id' => $userId,
            'novel_id' => $novelId,
        ]);

        return true;
    }

    public function down($novelId, $userId)
    {
        BookShelf::query()->where('user_id', $userId)
            ->where('novel_id', $novelId)
            ->delete();

        return true;
    }
}
